'use strict';
const mongoose = require('mongoose');
const config = require('../config/config.unittest.js')({});
const fs = require('fs');
const readline = require('readline');
const moment = require('moment');

let ParserBetter = require('../app/utils/rss/parser');
let parserBetter = new ParserBetter({timeout: 10000});

const UserModel = require('../app/model/User')({
  mongoose,
});
const TopicModel = require('../app/model/Topic')({
  mongoose,
});

const MessageModel = require('../app/model/Message')({
  mongoose,
});

mongoose.connect(config.mongoose.url, config.mongoose.options, function (err) {
  if (err) {
    console.error('connect to %s error: ', config.mongoose, err.message);
    process.exit(1);
  }
});

async function main() {

  let messages = await MessageModel.find().sort({_id: -1});
  for (const message of messages) {
    if (message.topic_id) {
      let topic = await TopicModel.findById(message.topic_id);
      if (!topic) {
        console.log(message.topic_id);
        await MessageModel.remove({topic_id: message.topic_id});
      }
    }
  }

  process.exit(0);
}

main();

